/**
 * 表单值域字典表管理初始化
 */
var SdsIndexField = {
    id: "SdsIndexFieldTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
SdsIndexField.initColumn = function () {
    return [[
        {title: '项目代码', field: 'itemCode', width: 50, align: 'center', valign: 'middle', sortable: true, editor:{
                type:'validatebox',
                options:{required:true}
            }},
        {title: '项目', field: 'itemId', width: 150, align: 'center', valign: 'middle', sortable: true, formatter:function(value,row,index){
                for(let j = 0,len=formItems.length; j < len; j++) {
                    for (let i = 0,len=formItems[j].children.length; i < len; i++){
                        if (formItems[j].id == 'singleDiseaseIndex') {
                            for (let k = 0; k < formItems[j].children[i].children.length; k++) {
                                if (value == formItems[j].children[i].children[k].id) {
                                    return formItems[j].children[i].children[k].text;
                                    break;
                                }
                            }
                            continue;
                        }
                        if (value == formItems[j].children[i].id) {
                            return formItems[j].children[i].text;
                            break;
                        }
                    }
                }
            }, editor:{
                type:'combotree',
                options:{required:true,valueField:'id',textField:'text',editable:false,
                    data: formItems
                }
            }}
    ]];
};

/**
 * 检查是否选中
 */
SdsIndexField.check = function () {
    var selected = $('#' + this.id).datagrid('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        SdsIndexField.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加新一行表格
 */
SdsIndexField.addFItem = function () {
    var rowNum=$('#' + SdsIndexField.id).datagrid("getRows").length;
    //添加一行
    $('#' + SdsIndexField.id).datagrid('appendRow',{
        itemCode: '',
        itemId: '',
        itemType: 1,
        entityId: $("#entityId").val(),
        indexHeaderId:$("#indexHeaderId").val()
    });
    var rowIndex= rowNum;
    $('#' + SdsIndexField.id).datagrid('selectRow', rowIndex).datagrid('beginEdit', rowIndex);
};

/**
 * 保存数据到库表
 */
SdsIndexField.saveFItem = function () {
    //保存完毕后，同步数据
    var operation_save = function(){
        SdsIndexField.table.acceptChanges();//调用datagrid自带的方法，确认其修改 然后保存至数据库
        var rows =  $('#' + SdsIndexField.id).datagrid('getRows');
        for (var i = 0; i < rows.length; i++) {
            var row = rows[i];
            if (row != undefined && row != {}) {
                if (row.id != undefined) {
                    //update Row
                    delete row.updateDate;
                    delete row.updateUser;
                    Sys.ajax(Sys.backendPath + "/sds/sdsindexheader/updateItem", row, function (data) {
                        Sys.success("修改成功!");
                    },function(data){
                        Sys.error("修改失败!" + data.responseJSON.message + "!");
                    }, "json", false, "修改项目保存至库中..");
                } else {
                    //add Row
                    Sys.ajax(Sys.backendPath + "/sds/sdsindexheader/addItem", row, function (data) {
                        Sys.success("添加成功!");
                    },function(data){
                        Sys.error("添加失败!" + data.responseJSON.message + "!");
                    }, "json", false, "新增项目保存至库中..");
                }
            }
        }
    };
    Sys.confirm("是否保存表格数据至数据库?",operation_save);
};

/**
 * 删除行同时删除数据库表数据
 */
SdsIndexField.delFItem = function () {
    if (SdsIndexField.check()) {
        var operation = function(){
            if(SdsIndexField.seItem.id!=null&&SdsIndexField.seItem.id!=""){
                var ajax = new $ax(Sys.backendPath + "/sds/sdsindexheader/deleteItem", function (data) {
                    Sys.success("删除成功!");
                    SdsIndexField.table.refresh();
                }, function (data) {
                    Sys.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.set("sdsIndexFieldId",SdsIndexField.seItem.id);
                ajax.start();
            }else{
                SdsIndexField.table.delRow();
            }
        };
        Sys.confirm("是否删除该行数据?",operation);
    }
};

/**
 * 保存表达式
 */
SdsIndexField.editExpress = function () {
    //提交信息
    var ajax = new $ax(Sys.backendPath + "/sds/sdsindexheader/updateExpress", function (data) {
        Sys.success("保存成功!");
    }, function (data) {
        Sys.error("保存失败!" + data.responseJSON.message + "!");
    });
    var express = encodeURIComponent($("#express").val());
    var indexHeaderId = $("#indexHeaderId").val();
    ajax.set("express",express);
    ajax.set("indexHeaderId",indexHeaderId);
    ajax.start();
};

/**
 * 查询表单值域字典表列表
 */
SdsIndexField.search = function () {
    var queryData = {};
    queryData['indexHeaderId'] = $("#indexHeaderId").val();
    queryData['itemType'] = 1;
    SdsIndexField.table.refresh({queryParams: queryData});
};

/**
 * 表单值域字典表管理初始化
 */
var SdsIndexFieldDen = {
    id: "SdsIndexFieldDenTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
SdsIndexFieldDen.initColumn = function () {
    return [[
        {title: '项目代码', field: 'itemCode', width: 50, align: 'center', valign: 'middle', sortable: true, editor:{
                type:'validatebox',
                options:{required:true}
            }},
        {title: '项目', field: 'itemId', width: 150, align: 'center', valign: 'middle', sortable: true, formatter:function(value,row,index){
                for(let j = 0,len=formItems.length; j < len; j++) {
                    for (let i = 0,len=formItems[j].children.length; i < len; i++){
                        if (value == formItems[j].children[i].id) {
                            return formItems[j].children[i].text;
                            break;
                        }
                    }
                }
            }, editor:{
                type:'combotree',
                options:{required:true,valueField:'id',textField:'text',editable:false,
                    data: formItems
                }
            }}
    ]];
};

/**
 * 检查是否选中
 */
SdsIndexFieldDen.check = function () {
    var selected = $('#' + this.id).datagrid('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        SdsIndexFieldDen.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加新一行表格
 */
SdsIndexFieldDen.addFItem = function () {
    var rowNum=$('#' + SdsIndexFieldDen.id).datagrid("getRows").length;
    //添加一行
    $('#' + SdsIndexFieldDen.id).datagrid('appendRow',{
        itemCode: '',
        itemId: '',
        itemType: 2,
        entityId: $("#entityId").val(),
        indexHeaderId:$("#indexHeaderId").val()
    });
    var rowIndex= rowNum;
    $('#' + SdsIndexFieldDen.id).datagrid('selectRow', rowIndex).datagrid('beginEdit', rowIndex);
};

/**
 * 保存数据到库表
 */
SdsIndexFieldDen.saveFItem = function () {
    //保存完毕后，同步数据
    var operation_save = function(){
        SdsIndexFieldDen.table.acceptChanges();//调用datagrid自带的方法，确认其修改 然后保存至数据库
        var rows =  $('#' + SdsIndexFieldDen.id).datagrid('getRows');
        for (var i = 0; i < rows.length; i++) {
            var row = rows[i];
            if (row != undefined && row != {}) {
                if (row.id != undefined) {
                    //update Row
                    delete row.updateDate;
                    delete row.updateUser;
                    Sys.ajax(Sys.backendPath + "/sds/sdsindexheader/updateItem", row, function (data) {
                        Sys.success("修改成功!");
                    },function(data){
                        Sys.error("修改失败!" + data.responseJSON.message + "!");
                    }, "json", false, "修改项目保存至库中..");
                } else {
                    //add Row
                    Sys.ajax(Sys.backendPath + "/sds/sdsindexheader/addItem", row, function (data) {
                        Sys.success("添加成功!");
                    },function(data){
                        Sys.error("添加失败!" + data.responseJSON.message + "!");
                    }, "json", false, "新增项目保存至库中..");
                }
            }
        }
    };
    Sys.confirm("是否保存表格数据至数据库?",operation_save);
};

/**
 * 删除行同时删除数据库表数据
 */
SdsIndexFieldDen.delFItem = function () {
    if (SdsIndexFieldDen.check()) {
        var operation = function(){
            if(SdsIndexFieldDen.seItem.id!=null&&SdsIndexFieldDen.seItem.id!=""){
                var ajax = new $ax(Sys.backendPath + "/sds/sdsindexheader/deleteItem", function (data) {
                    Sys.success("删除成功!");
                    SdsIndexFieldDen.table.refresh();
                }, function (data) {
                    Sys.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.set("SdsIndexFieldDenId",SdsIndexFieldDen.seItem.id);
                ajax.start();
            }else{
                SdsIndexFieldDen.table.delRow();
            }
        };
        Sys.confirm("是否删除该行数据?",operation);
    }
};

/**
 * 保存表达式
 */
SdsIndexFieldDen.editExpress = function () {
    //提交信息
    var ajax = new $ax(Sys.backendPath + "/sds/sdsindexheader/updateExpress", function (data) {
        Sys.success("保存成功!");
    }, function (data) {
        Sys.error("保存失败!" + data.responseJSON.message + "!");
    });
    var expressDen = encodeURIComponent($("#expressDen").val());
    var indexHeaderId = $("#indexHeaderId").val();
    ajax.set("expressDen",expressDen);
    ajax.set("indexHeaderId",indexHeaderId);
    ajax.start();
};

/**
 * 查询表单值域字典表列表
 */
SdsIndexFieldDen.search = function () {
    var queryData = {};
    queryData['indexHeaderId'] = $("#indexHeaderId").val();
    queryData['itemType'] = 2;
    SdsIndexFieldDen.table.refresh({queryParams: queryData});
};

$(function () {
    var indexHeaderId = $("#indexHeaderId").val();
    var defaultColunms = SdsIndexField.initColumn();
    var table = new HISUITable(SdsIndexField.id, Sys.backendPath + '/sds/sdsindexheader/listItem/' + indexHeaderId + "/1", defaultColunms);
    table.setPaginationType("server");
    table.setSubHeight(-30);
    SdsIndexField.table = table.init();

    var denColunms = SdsIndexFieldDen.initColumn();
    var denTable = new HISUITable(SdsIndexFieldDen.id, Sys.backendPath + '/sds/sdsindexheader/listItem/' + indexHeaderId + "/2", denColunms);
    denTable.setPaginationType("server");
    denTable.setSubHeight(-30);
    SdsIndexFieldDen.table = denTable.init();

    //SdsIndexField.search();
});

